﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using AnimatedGif;//nuget 安装animateGif库

namespace vlcplay
{
    public partial class FuseGif : Form
    {
        Image imgdisplay = null;
        
        public FuseGif()
        {
            InitializeComponent();
            comboBox_format.SelectedIndex = 0;
        }

        private void button_file_Click(object sender, EventArgs e)
        {
            textBox_filename.Text = string.Empty;

            #region 选择文件夹
            //OpenFileDialog sfd = new OpenFileDialog(); // 选择文件
            //默认显示名称
            //sfd.FileName = "sqlite.db";
            //设置匹配文件格式，可包含多种
            //sfd.Filter = "Excel(*.xlsx)|*.xlsx";
            //sfd.Filter = "数据库文件(*.db)|*.db";

            FolderBrowserDialog sfd = new FolderBrowserDialog();
            sfd.Description = "选择文件夹";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                //textBox_filename.Text = sfd.FileName;//文件路径加文件名
                textBox_filename.Text = sfd.SelectedPath;//文件夹路径
            }
            else
            {
                return;
            }
            #endregion
        }

        private void button_fuse_Click(object sender, EventArgs e)
        {
            try
            {
                int delay = 100;
                if (imgdisplay != null)
                {
                    imgdisplay.Dispose();
                }
                if (textBox_filename.Text == "")
                {
                    MessageBox.Show("请选择图片文件夹");
                    return;
                }
                if (textBox_delay.Text != "")
                {
                    delay = Convert.ToInt32(textBox_delay.Text.Trim());
                }
                // Application.StartupPath: /bin/debug 文件夹路径
                string path = "";// textBox_filename.Text + "\\new.gif";//生成gif路径加名称
                string filemale = textBox_filename.Text + "\\new";
                bool fileExists = true;
                while (fileExists)
                {
                    filemale += "1";
                    path = filemale + ".gif";//生成gif路径加名称
                    fileExists = File.Exists(path);
                }
                List<string> imgs = Directory.GetFiles(textBox_filename.Text, "*." + comboBox_format.Text).ToList();//using System.IO;
                if (imgs.Count <= 0)
                {
                    pictureBox_img.Image = null;
                    MessageBox.Show("未找到相应格式的文件，请确认文件格式");
                    return;
                }
                /*imgs.Sort((a, b) =>//排序好像有点问题 获取的数值不对
                {
                    string reg = "[0-9]+";
                    int a_index = int.Parse(Regex.Match(a, reg).Value);
                    int b_index = int.Parse(Regex.Match(b, reg).Value);//using System.Text.RegularExpressions;
                    return a_index > b_index ? 1 : a_index < b_index ? -1 : 0;
                });*/
                var img = Image.FromFile(imgs[0]);
                using (var gif = AnimatedGif.AnimatedGif.Create(path, delay))
                {
                    for (int i = 0; i < imgs.Count; i++)
                    {
                        img = Image.FromFile(imgs[i]);
                        gif.AddFrame(img, delay: -1, quality: GifQuality.Bit8);
                    }
                }
                imgdisplay = Image.FromFile(path);
                pictureBox_img.Image = imgdisplay;
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine("参数错误:"+ex.Message);
            }
        }

        private void textBox_delay_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != (char)8)
            {
                e.Handled = true; // 不允许输入其他字符
                MessageBox.Show("仅允许输入数字：0~99999");
            }
        }
    }
}